﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using Dapper;

namespace AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service
{
    public static class SmdStoreService
    {
        public static SmdStore GetByAuthId(string authId)
        {
            SmdStore smdStore = null;
            using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
            {
                smdStore = conn.Query<SmdStore>("SELECT * FROM t_smd_Store WHERE AuthID=@AuthID", new { AuthID = authId }).FirstOrDefault();
            }
            return smdStore;
        }

        public static IEnumerable<SmdStore> GetByDealerId(int dealerId)
        {
            using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
            {
                var smdStores = conn.Query<SmdStore>("SELECT * FROM t_smd_Store WHERE DealerID=@DealerID",
                    new { DealerID = dealerId }).ToList();

                return smdStores;
            }
        }

        public static string GetStoreTypeCode(string storeType)
        {
            if (string.IsNullOrEmpty(storeType))
            {
                throw new Exception("GetStoreTypeCode Error: storeType must have value");
            }
            var types = new List<string> { "DCSS", "DDCN", "MT", "HDC", "LCSS", "MASS", "Store" };

            foreach (var type in types.Where(storeType.Contains))
            {
                return type;
            }

            if (storeType.Contains("多乐士"))
            {
                return "DIS";
            }
            if (storeType.Contains("来威"))
            {

                return "LIS";
            }

            return storeType;
        }

        public static void UpdateStore(string authId, string serialnumber)
        {
            using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
            {
                conn.Execute("UPDATE t_smd_Store SET Eapprovesn=@Eapprovesn WHERE AuthID=@AuthID",
                             new { Eapprovesn = serialnumber, AuthID = authId });
            }
        }


        //#region 原方法
        //public static void FormatBeforeType(Tinting tinting, Store store)
        //{
        //    using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
        //    {
        //        if (tinting.NewFitmentName.Contains("翻新"))
        //        {
        //            store.LastRenewDate = DateTime.Now;
        //            store.IsRenew = true;
        //            var reader =
        //                conn.ExecuteReader(
        //                    "select StoreType from [t_smd_Store] WHERE AuthID = @AuthID", new { store.AuthID });

        //            if (reader.Read())
        //            {
        //                store.BeforeRenewType = reader.GetString(0);
        //            }
        //        }
        //        else if (tinting.NewFitmentName.Contains("升级"))
        //        {
        //            store.IsUpgraded = true;
        //            var reader =
        //                conn.ExecuteReader(
        //                    "select StoreType from [t_smd_Store] WHERE AuthID = @AuthID", new { store.AuthID });


        //            // 主数据不存在，默认为“DIS”，否则门店类型与变更前门店类型为主数据库当前门店的类型
        //            if (reader.Read())
        //            {
        //                store.BeforeUpgradedType = reader.GetString(0);
        //                store.StoreType = reader.GetString(0);
        //            }
        //            else
        //            {
        //                store.BeforeUpgradedType = "DIS";
        //                store.StoreType = "DIS";
        //            }
        //        }
        //    }
        //}
        //#endregion

        #region Tony 更新方法
        public static void FormatBeforeType(SyncPoolModel tinting, Store store)
        {
            using (var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME))
            {
                if (tinting.NewFitmentName.Contains("翻新"))
                {
                    store.LastRenewDate = DateTime.Now;
                    store.IsRenew = true;
                    var reader =
                        conn.ExecuteReader(
                            "select StoreType from [t_smd_Store] WHERE AuthID = @AuthID", new { store.AuthID });

                    if (reader.Read())
                    {
                        store.BeforeRenewType = reader.GetString(0);
                    }
                }
                else if (tinting.NewFitmentName.Contains("升级"))
                {
                    store.IsUpgraded = true;
                    var reader =
                        conn.ExecuteReader(
                            "select StoreType from [t_smd_Store] WHERE AuthID = @AuthID", new { store.AuthID });


                    // 主数据不存在，默认为“DIS”，否则门店类型与变更前门店类型为主数据库当前门店的类型
                    if (reader.Read())
                    {
                        store.BeforeUpgradedType = reader.GetString(0);
                        store.StoreType = reader.GetString(0);
                    }
                    else
                    {
                        store.BeforeUpgradedType = "DIS";
                        store.StoreType = "DIS";
                    }
                }
            }
        }
        #endregion
    }
}
